package ac.github.oa.internal.core.attribute

import ac.github.oa.api.event.entity.EntityDamageEvent
import ac.github.oa.api.event.plugin.OriginPluginReloadEvent
import ac.github.oa.internal.base.enums.PriorityEnum
import ac.github.oa.internal.base.event.impl.DamageMemory
import org.bukkit.Sound
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Player
import org.bukkit.entity.Silverfish
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common.platform.event.EventPriority
import taboolib.common.platform.event.SubscribeEvent
import taboolib.common.platform.function.info
import taboolib.common.platform.function.submit
import taboolib.common5.compileJS
import taboolib.platform.util.sendActionBar
import javax.script.CompiledScript

object DamageHandler {

    var handlerScript: CompiledScript? = null

    @Awake(LifeCycle.ENABLE)
    fun compile() {
        handlerScript = AttributeManager.config.getString("damage-handler")?.compileJS()
    }

    @SubscribeEvent
    fun e(e: OriginPluginReloadEvent) {
        this.compile()
    }

    @SubscribeEvent(ignoreCancelled = true, priority = EventPriority.MONITOR)
    fun e(e: EntityDamageEvent) {
        if (!e.isCancelled && !e.damageMemory.event.isCancelled && e.priorityEnum == PriorityEnum.POST) {
            if (handlerScript != null) {
                try {
                    handlerScript?.eval(handlerScript!!.engine.createBindings().also {
                        it["openAPI"] = DamageEventOpenAPI(e)
                    })
                } catch (error: Exception) {
                    error.printStackTrace()
                    info("error ${e.damageMemory}")

                }
            }

        }
    }

    class DamageEventOpenAPI(val e: EntityDamageEvent) {

        // 取标签值
        fun getLabel(key: String): Any? {
            return e.damageMemory.labels[key]
        }

        // 目标标签值是否存在
        fun hasLabel(key: String): Boolean {
            return e.damageMemory.labels.containsKey(key)
        }

        // 打印
        fun println(vararg any: Any) {
            info(any)
        }

        fun sendTitle(entity: LivingEntity, title: String, subTitle: String, fadeIn: Int, stay: Int, fadeOut: Int) {
            if (entity is Player) {
                entity.sendTitle(title, subTitle, fadeIn, stay, fadeOut)
            }
        }

        fun getSound(type: String): Sound {
            return Sound.valueOf(type)
        }

        fun tell(entity: LivingEntity, message: String) {

        }

        // 攻击者
        val attacker: LivingEntity
            get() = e.damageMemory.attacker

        // 被攻击者
        val defender: LivingEntity
            get() = e.damageMemory.injured

        // 总伤害
        val totalDamage: Double
            get() = e.damageMemory.totalDamage

        // 伤害来源
        val damageSources: List<DamageMemory.Source>
            get() = e.damageMemory.getDamageSources()

        // 发送 action bar消息
        fun sendAction(player: Player, message: String) {
            player.sendActionBar(message)
        }

    }


}
